package com.eight.lesson2;

import java.util.ArrayList;
import java.util.List;

public class AppleTest {

	public static void main(String[] args) {
		Apple apple1 = new Apple(150, "red");
		Apple apple2 = new Apple(130, "green");
		Apple apple3 = new Apple(110, "yellow");
		
		List<Apple> apples = new ArrayList<Apple>();
		apples.add(apple1);
		apples.add(apple2);
		apples.add(apple3);
		
		// 匿名类
		List<Apple> result = AppleUtils.filterApples(apples, new ApplePredicate() {
			
			@Override
			public boolean test(Apple apple) {
				return apple.getWeight()>110;
			}
		});
		System.out.println("匿名类："+result.size());
		
		// Lambda
		List<Apple> result2 = AppleUtils.filterApples(apples, (Apple apple) -> apple.getWeight()>110);
		System.out.println("Lambda: "+result2.size());
		
		// 泛型抽象
		List<Apple> result3 = AppleUtils.filter(apples, (Apple apple) -> apple.getWeight()>110);
		System.out.println("Lambda-T: "+result3.size());
		
		List<Integer> numbers = new ArrayList<Integer>();
		numbers.add(1);
		numbers.add(2);
		numbers.add(3);
		List<Integer> result4 = AppleUtils.filter(numbers, (Integer i) -> i%2==0);
		System.out.println("Lambda-T: "+result4.size());
		
		// 用Comparator来排序
		System.out.println("用Comparator来排序");
		System.out.println("-------------排序前-----------------");
		for(Apple apple : apples) {
			System.out.println(apple.getColor());
		}
		apples.sort((Apple a1, Apple a2) -> a1.getColor().compareTo(a2.getColor()));
		System.out.println("-------------排序后-------------");
		for(Apple apple : apples) {
			System.out.println(apple.getColor());
		}
		
		// 用Runnable执行代码块
		System.out.println("用Runnable执行代码块");
		Thread t = new Thread(() -> System.out.println("Hello, World"));
		t.start();
	}

}
